//import org.apache.log4j.Logger;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import org.apache.logging.log4j.LogManager;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.Key;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

class ReadNc{
    private String filename;
    private String varname;
    private List<String> varnames;
    public HashMap hashmap = new HashMap<>();
    private List<String> varAll;
    public List<Variable> variablelist;
//    public static Logger logger1 = Logger.getLogger(DataDemo.class);

    public ReadNc(String filename) throws IOException {
        this(filename, "", new ArrayList<String>());
    }
    public ReadNc(String filename, String varname) throws IOException {
        this(filename, varname, new ArrayList<String>());
    }

    ReadNc(String filename, List varnames) throws IOException {
        this(filename, "", varnames);
    }

    public ReadNc(String filename, String varname, List<String> varnames) throws IOException {
        this.filename = filename;
        this.varname = varname;
        this.varnames = varnames;
        this.readfile();
        }

    public void readfile() throws IOException {
        NetcdfFile open = NetcdfFile.open(this.filename);
        this.variablelist = open.getVariables();
        if(!this.varname.equals("")){
            varnames.add(this.varname);
        }
        if (this.varnames.isEmpty()) {
            this.varnames = this.showShortName();
        }
        for (Variable var : this.variablelist) {
            if (this.varnames.contains(var.getShortName())) {
                this.hashmap.put(var.getShortName(),  var.read().copyToNDJavaArray());
            }
        }
    }
    public List<String> showShortName(){
        this.varAll = new ArrayList<String>();
        for (Variable var: this.variablelist){
            this.varAll.add(var.getShortName());
        }
        System.out.println(this.varAll);
        return this.varAll;
    }
    public String getVarname(){return this.varname;}



}
public class DataDemo {
    public static void main(String[] args) throws IOException {
//        ReadNc readNc = new ReadNc("/Users/xujimao/IdeaProjects/datademo/src/main/data/icec.mon.ltm.1981-2010.nc", "lat");
//        readNc.readfile();
//        System.out.println(readNc.hashmap.get("lat"));
        Date date = new Date();
        System.out.println(date.getTime());
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date);
        String s = LocalDateTime.now().toString();
        System.out.println(s);
        System.out.println(format);

    }
}
